﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.DataAccess
{
    public class ActionDataAccess
    {
        #region 单例模式
        private ActionDataAccess()
        {
        }

        private static ActionDataAccess _instance;
        private static readonly object _syncObject = new object();

        public static ActionDataAccess Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new ActionDataAccess();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion 单例模式

        /// <summary>
        /// 添加功能
        /// </summary>
        /// <param name="actionName">功能名称</param>
        /// <param name="controllName">控制器名称</param>
        /// <param name="description">功能描述</param>
        /// <param name="createTime">创建时间</param>
        /// <param name="isDelete">删除标识</param>
        /// <param name="remark">备注</param>
        /// <param name="parentId">父功能Id</param>
        /// <returns>返回添加是否成功</returns>
        public int AddAction(string actionName, string controllName, string description, string createTime, int isDelete, string remark, int parentId)
        {
            int ret = 0;
            try
            {
                DBHelper db = DBFactory.AdminDB;
                string strSql = @"insert into Action (ActionName,ControllerName,Description,CreateTime,IsDelete,Remark,ParentID) 
                                values (@ActionName,@ControllName,@Description,@CreateTime,@IsDelete,@Remark,@ParentID)";
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@ActionName", DbType.String, actionName);
                db.AddInParameter(Procdbcomm, "@ControllName", DbType.String, controllName);
                db.AddInParameter(Procdbcomm, "@Description", DbType.String, description);
                db.AddInParameter(Procdbcomm, "@CreateTime", DbType.DateTime, createTime);
                db.AddInParameter(Procdbcomm, "@IsDelete", DbType.Int32, isDelete);
                db.AddInParameter(Procdbcomm, "@Remark", DbType.String, remark);
                db.AddInParameter(Procdbcomm, "@ParentID", DbType.Int32, parentId);
                ret = db.ExecuteNonQuery(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                ret = -99;
                LogHelper.Error("添加功能-AddAction" + ex.ToString());
            }
            return ret;
        }

        /// <summary>
        /// 修改功能
        /// </summary>
        /// <param name="actionId">功能编号</param>
        /// <param name="actionName">功能名称</param>
        /// <param name="description">功能描述</param>
        /// <param name="controllName">控制器名称</param>
        /// <param name="remark">备注</param>
        /// <param name="parentId">父功能Id</param>
        /// <returns></returns>
        public int EditAction(int actionId, string actionName, string controllName, string description, string remark, int parentId)
        {
            int ret = 0;
            try
            {
                DBHelper db = DBFactory.AdminDB;
                string strSql = @"update Action set ActionName=@ActionName,ControllerName=@ControllName,Description=@Description,
                                    Remark=@Remark,ParentID=@ParentID  where ActionID=@ActionID";
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@ActionName", DbType.String, actionName);
                db.AddInParameter(Procdbcomm, "@ControllName", DbType.String, controllName);
                db.AddInParameter(Procdbcomm, "@Description", DbType.String, description);
                db.AddInParameter(Procdbcomm, "@Remark", DbType.String, remark);
                db.AddInParameter(Procdbcomm, "@ParentID", DbType.Int32, parentId);
                db.AddInParameter(Procdbcomm, "@ActionID", DbType.Int32, actionId);
                ret = db.ExecuteNonQuery(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                ret = -99;
                LogHelper.Error("修改功能-EditAction" + ex.ToString());
            }
            return ret;
        }

        /// <summary>
        /// 修改功能
        /// </summary>
        /// <param name="actionId">功能编号</param>
        /// <param name="controllName">控制器名称</param>
        /// <param name="description">功能描述</param>
        /// <param name="remark">备注</param>
        /// <param name="parentId">父功能Id</param>
        /// <returns>返回是否修改成功</returns>
        public int EditAction(int actionId, string controllName, string description, string remark, int parentId)
        {
            int ret = 0;
            try
            {
                DBHelper db = DBFactory.AdminDB;
                string strSql = @"update Action set ControllerName=@ControllName,Description=@Description,Remark=@Remark,
                                     ParentID=@ParentID where ActionID=@ActionID";
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@ControllName", DbType.String, controllName);
                db.AddInParameter(Procdbcomm, "@Description", DbType.String, description);
                db.AddInParameter(Procdbcomm, "@Remark", DbType.String, remark);
                db.AddInParameter(Procdbcomm, "@ParentID", DbType.Int32, parentId);
                db.AddInParameter(Procdbcomm, "@ActionID", DbType.Int32, actionId);
                ret = db.ExecuteNonQuery(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                ret = -99;
                LogHelper.Error("修改功能-EditAction" + ex.ToString());
            }
            return ret;
        }

        /// <summary>
        /// 根据功能编号获得功能信息
        /// </summary>
        /// <param name="actionId">功能编号</param>
        /// <returns></returns>
        public DataTable GetActionInfo(int actionId)
        {
            DataTable dt = new DataTable();
            try
            {
                DBHelper db = DBFactory.AdminDB;
                string strSql = "select * from Action where IsDelete=0 and ActionID=@ActionID";
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@ActionID", DbType.Int64, actionId);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                {
                    dt = ds.Tables[0];
                }
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("根据功能编号获得功能信息-GetActionInfo" + ex.ToString());
            }
            return dt;
        }

        /// <summary>
        /// 根据功能名称获取功能信息
        /// </summary>
        /// <param name="actionName">action名称</param>
        /// <returns></returns>
        public DataTable GetActionInfo(string actionName,string controllerName)
        {
            DataTable dt = new DataTable();

            try
            {
                DBHelper db = DBFactory.AdminDB;
                string strSql = "select * from Action where IsDelete=0 and ActionName=@ActionName and controllerName=@controllerName";
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@ActionName", DbType.String, actionName);
                db.AddInParameter(Procdbcomm, "@controllerName", DbType.String, controllerName);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                {
                    dt = ds.Tables[0];
                }
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("根据功能名称获取功能信息-GetActionInfo" + ex.ToString());
            }
            return dt;
        }

        /// <summary>
        /// 根据条件筛选查询功能信息
        /// </summary>
        /// <param name="actionName"></param>
        /// <param name="controllName"></param>
        /// <param name="description">功能描述</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageRows"></param>
        /// <returns></returns>
        public DataSet GetPageList(string actionName, string controllName, string description, string parentName, int pageIndex, int pageRows)
        {
            DataSet ds = new DataSet();
            try
            {
                DBHelper db = DBFactory.AdminDB;
                DbCommand Procdbcomm = db.GetStoredProcCommand("proc_action_getlist");
                db.AddInParameter(Procdbcomm, "@actionName", DbType.String, actionName);
                db.AddInParameter(Procdbcomm, "@controllName", DbType.String, controllName);
                db.AddInParameter(Procdbcomm, "@description", DbType.String, description);
                db.AddInParameter(Procdbcomm, "@parentName", DbType.String, parentName);
                db.AddInParameter(Procdbcomm, "@pageIndex", DbType.Int32, pageIndex);
                db.AddInParameter(Procdbcomm, "@pageRows", DbType.Int32, pageRows);
                ds = db.ExecuteDataSet(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("根据条件筛选查询功能信息-GetPageList" + ex.ToString());
            }
            return ds;
        }

        /// <summary>
        /// 查询所有功能信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllActionList()
        {
            DataTable dt = new DataTable("Action");
            try
            {
                DBHelper db = DBFactory.AdminDB;
                string strSql = "select * from Action where IsDelete=0";
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                {
                    dt = ds.Tables[0];
                }
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("查询所有功能信息-GetAllActionList" + ex.ToString());
            }
            return dt;
        }

        /// <summary>
        /// 删除功能（逻辑删除）
        /// </summary>
        /// <param name="menuId">action编号</param>
        /// <returns></returns>
        public int DeleteAction(int actionId)
        {
            int ret = 0;
            try
            {
                DBHelper db = DBFactory.AdminDB;
                string strSql = "update Action set IsDelete=1 where ActionID=@ActionID";
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@ActionID", DbType.Int32, actionId);
                ret = db.ExecuteNonQuery(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                ret = -99;
                LogHelper.Error("删除功能-DeleteAction" + ex.ToString());
            }
            return ret;
        }

        /// <summary>
        /// 获取所有父功能功能信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllParentActionList()
        {
            DataTable dt = new DataTable("Action");
            try
            {
                DBHelper db = DBFactory.AdminDB;
                string strSql = "select * from Action where IsDelete = 0 and (ActionName = '#' or ControllerName = '#')";
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                {
                    dt = ds.Tables[0];
                }
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取所有父功能功能信息-GetAllParentActionList" + ex.ToString());
            }
            return dt;
        }
    }
}
